package com.yulebaby.callsenter.task;

import android.Manifest;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.provider.CallLog;
import android.support.v4.app.ActivityCompat;
import android.util.Log;

import com.orhanobut.logger.Logger;
import com.yulebaby.callsenter.bean.CallLogBean;
import com.yulebaby.callsenter.global.App;
import com.yulebaby.callsenter.utils.SPUtil;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 读取通话记录
 * Author: Zhangbo
 * Time: 2016/11/13
 */

public class ReadContactTask {

    public static void getCallLog() {
        String[] projection = {CallLog.Calls.CACHED_NAME, CallLog.Calls.NUMBER,
                CallLog.Calls.TYPE, CallLog.Calls.DURATION, CallLog.Calls.DATE};
        try {
            Uri callLogUri = CallLog.Calls.CONTENT_URI;
            if (ActivityCompat.checkSelfPermission(App.mContext, Manifest.permission.READ_CALL_LOG) != PackageManager.PERMISSION_GRANTED) {
            }
            Cursor cursor = App.mContext.getContentResolver().query(callLogUri, projection, null, null, CallLog.Calls.DEFAULT_SORT_ORDER);
            String callLogName;
            String callLogNumber = null;
            String callLogDate = null;
            int callLogType = 0;
            int callLogTime = 0;
            String type;
            int time;
            while (cursor.moveToNext()) {
                callLogName = cursor.getString(cursor.getColumnIndex(CallLog.Calls.CACHED_NAME));
                if (callLogName == null) {
                    callLogName = "未知号码";
                }
                callLogNumber = cursor.getString(cursor.getColumnIndex(CallLog.Calls.NUMBER));

                callLogDate = cursor.getString(cursor.getColumnIndex(CallLog.Calls.DATE));
                SimpleDateFormat DateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd_HH_mm");
                long date = Long.parseLong(callLogDate);
                Date d = new Date(date);
                callLogDate = DateFormat.format(d);

                callLogType = cursor.getInt(cursor.getColumnIndex(CallLog.Calls.TYPE));
                if (callLogType == 1) {
                    type = "呼入";
                } else if (callLogType == 2) {
                    type = "呼出";
                } else
                    type = "未接通";

                callLogTime = cursor.getInt(cursor.getColumnIndex(CallLog.Calls.DURATION));
                if (type == "未接通") {
                    time = 0;
                } else {
                    time = callLogTime;
                }
                int playState = 0;
                String filePath = (String) SPUtil.get(formatter.format(d), "");
                if (!filePath.equals("")) {
                    playState = 2;
                }
            }
            Logger.d(callLogDate + "------" + callLogNumber);
            cursor.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
